/* SVN FILE: $Id: cookie.js 11 2008-06-22 17:15:42Z rafaelbandeira3 $ */
/**
* Application : The Javascript MVC Framework
*
* @package application.core
* @subpackage cookie
* @author 			$Author: rafaelbandeira3 $
* @copyright 		(c) Rafael Bandeira, Marcelo Condotta, Henrique Condotta. All Rights Reserved.
* @version 			$Revision: 11 $
* @lastrevision 	$Date: 2008-06-22 17:15:42 +0000 (Sun, 22 Jun 2008) $
* @modifiedby 		$LastChangedBy: rafaelbandeira3 $
* @lastmodified 	$LastChangedDate: 2008-06-22 17:15:42 +0000 (Sun, 22 Jun 2008) $
* @license 			The MIT License [http://www.opensource.org/licenses/mit-license.php]
* @filesource 		$URL: http://jsmvcapplication.googlecode.com/svn/trunk/application/src/core/cookie.js $
*/
var Cookie = new (function()
{
	var getCookies = function()
	{
		var cookies = {};
		if(document.cookie.length != 0)
		{
			var pairs = document.cookie.split(';')
			var pairsCount = pairs.length;
			for(i=0;i<pairsCount;i++)
			{
				data = pairs[i].split('=');
				key = data[0].strip();
				value = data[1].strip();
				cookies[key] = value;
			}
		}
		return cookies;		
	}

	this.create = function(name,value,expires,path)
	{
		if(typeof expires == 'undefined') expires = '1 hour';
		if(!path) path = '/';

		var cookieString, cookieData = {}, cookie = new Array(), date = new Date();
		var dataOrder = [name,'expires','path'];

		cookieData[name] = value;
		cookieData.expires = date.sum(expires).toGMTString();
		cookieData.path = path;

		for(prop in cookieData)
		{
			pos = dataOrder.indexOf(prop);
			cookie[pos] = prop + '=' + cookieData[prop];
		}

		cookieString = cookie.join(';');
		document.cookie = cookieString;
	}

	this.read = function(name)
	{
		var cookies = getCookies();
		if(typeof cookies[name] != 'undefined')
			return cookies[name];
		else
			return null;
	}

	this.isSet = function(name)
	{
		var cookies = getCookies();
		return !!cookies[name];
	}

	this.erase = function(name)
	{
		return this.create(name,'',-1);
	}

	this.cleanUp = function()
	{
		var cookies = getCookies();
		for(i in cookies)
			this.erase(i);
		return true;
	}
})();
